<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新建药品类别</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" ref="table">
      <el-table-column label="药品类别名称" align="center" prop="categoryName" />
      <el-table-column label="处方类型" align="center" prop="prescriptionType">
        <template slot-scope="{ row }">
          <dict-tag :options="dict.type.tcm_prescript_type" :value="row.prescriptionType" />
        </template>
      </el-table-column>
      <el-table-column label="是否发送配药机" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.machinePush ? '是' : '否' }}</span>
        </template>
        </el-table-column>
      <!-- <el-table-column label="是否允许小数点" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.isDecimal ? '是' : '否' }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" prop="status" width="140">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            active-text="启用"
            :inactive-value="1"
            inactive-text="禁用"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditDrugCalss ref="EditDrugCalss" @refresh="getList" />
  </div>
</template>

<script>
import { getDrugCategoryList, deleteDrugCategory, updateDrugCategory } from "@/api/saas/tcm.js";
import EditDrugCalss from './editDrugCalss'

export default {
  name: "DrugClass",
  dicts: [
    "tcm_prescript_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
      },
      // 总条数
      total: 0,
      dataList: [],
    };
  },
  components: {
    EditDrugCalss
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getDrugCategoryList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(row) {
      this.$refs.EditDrugCalss.openModal(row);
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该药品吗？")
        .then(function () {
          return deleteDrugCategory({id});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 修改状态
    changeStatus(row) {
      updateDrugCategory(row).then(res=>{
        this.$message.success('状态修改成功');
      }).catch((err)=>{
        row.status = row.status ? 0 : 1; // 修改失败,复位
      })
    }
  },
};
</script>

<style lang="scss" scoped></style>
